package net.lanelife.casino.caipiao.service;

import java.util.Map;

import javax.persistence.Query;

import net.lanelife.casino.caipiao.entity.statistics.CompanyStatistics;
import net.lanelife.casino.caipiao.repository.api.ICompanyStatisticsRepository;
import net.lanelife.casino.caipiao.service.api.ICompanyStatisticsService;
import net.lanelife.casino.common.repository.support.GenericRepository;
import net.lanelife.casino.common.service.support.GenericService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CompanyStatisticsService extends GenericService<CompanyStatistics, Integer> implements ICompanyStatisticsService {

	@Autowired
	private ICompanyStatisticsRepository repository;
	
	public CompanyStatisticsService() {
		super(CompanyStatistics.class);
	}

	@Override
	protected GenericRepository<CompanyStatistics, Integer> getRepository() {
		return repository;
	}

	@Override
	public int update(Map<String, Object> properties) throws Exception {
		StringBuilder sql = new StringBuilder("UPDATE TB_COMPANY_STATISTICS SET VERSION=VERSION+1,");
		for (String property : properties.keySet()) {
			sql.append(property).append("=").append(property).append("+:").append(property).append(",");
		}
		sql.replace(sql.length()-1, sql.length(), " ");
		Query query = em.createNativeQuery(sql.toString());
		for (String property : properties.keySet()) {
			query.setParameter(property, properties.get(property));
		}
		return query.executeUpdate();
	}

}
